/* 
  License : GPLv3
  Author  : William Wong
  E-mail  : hzwhuang@gmail.com
  Language: C
*/
#include "menu.h"
#include "uniproxy-gui.h"
#include "pybackend.h"
#include "detect.h"
#include <stdlib.h>
#include <glib/gi18n.h>
#define CHARSET "utf-8"

extern GPid backend_pid;
gboolean show_or_hide(GtkWidget *wid, GdkEvent *evnet, gpointer data)
{
	gboolean visible;
	g_object_get(window, "visible", &visible, NULL);
	if(visible)		gtk_widget_hide(window);
	else	gtk_widget_show(window);
	return TRUE;
}

void e_proxy(GtkWidget *wid, gpointer data)
{
	check_gae();
	if(app_exist && backend_pid <= 0){
		if(execute_backend()){
			gchar buf[300];
			gtk_label_set_markup(GTK_LABEL(label), _("<b>Running...</b>"));
			gtk_button_set_label(GTK_BUTTON(lbutton), _("Stop"));
		}
		else
		{
			gtk_label_set_markup(GTK_LABEL(label), _("Execution Failed."));
		}
	}
	else{
		if(app_exist)	
		{
			close_backend();
			gtk_label_set_markup(GTK_LABEL(label), _("Stopped."));
			gtk_button_set_label(GTK_BUTTON(lbutton), _("Start"));
		}
		else	gtk_label_set_markup(GTK_LABEL(label), _("Backend app doesn't exist."));
	}
}

gboolean show_quit(GtkWidget *wid, GdkEvent *event, gpointer data)
{
	GtkWidget *dialog;
	GtkWidget *label;
	GtkWidget *content_area;
	dialog = gtk_dialog_new_with_buttons(_("Exit?"), GTK_WINDOW(window),
											GTK_DIALOG_MODAL,GTK_STOCK_CANCEL,GTK_RESPONSE_REJECT,GTK_STOCK_OK,GTK_RESPONSE_OK,NULL);
	label = gtk_label_new (_("Are you sure to exit Uniproxy-GUI?\n\nProxy daemon will be killed."));
	content_area = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
	gtk_container_add (GTK_CONTAINER (content_area), label);
	gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE);
	gtk_widget_show_all(dialog);
	gint choose;
	choose = gtk_dialog_run(GTK_DIALOG(dialog));
	if(choose == GTK_RESPONSE_OK){
		close_backend();
		g_key_file_free(uniconfig);
		gtk_main_quit();
	}
	else if(choose == GTK_RESPONSE_REJECT)	gtk_widget_destroy(dialog);
	return TRUE;
}

int main(int argc, char *argv[])
{
	GtkWidget *vbox;
	GtkWidget *hbox;
	GtkWidget *button;
	GtkStatusIcon *tray_icon;
	GtkWidget *menubar;
	GtkWidget *menu_menu;
	GtkWidget *menu;
	GtkWidget *quit;
	GtkWidget *setup;
	GtkWidget *help;
	GtkWidget *help_menu;
	GtkWidget *about;
	gtk_init (&argc, &argv);
	
	//Main Window
	window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	gtk_container_set_border_width (GTK_CONTAINER (window), 5);
	gtk_window_set_title (GTK_WINDOW (window), "Uniproxy-GUI");
	gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
	gtk_widget_realize (window);
	gtk_widget_set_size_request(window, 320,240);
	gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
	g_signal_connect(G_OBJECT(window), "delete-event", G_CALLBACK(show_quit), NULL);

	//Init
	bindtextdomain ("uniproxy-gui", "/usr/share/locale");
	bind_textdomain_codeset ("uniproxy-gui", CHARSET);
	textdomain ("uniproxy-gui");
	
	if(!g_thread_get_initialized ())	g_thread_init (NULL);
	uniconfig = g_key_file_new();
	flags = G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS;
	user_home = malloc(strlen(getenv("HOME"))+26);
	strcpy(user_home, getenv("HOME"));
	strcat(user_home, "/.uniconfig");
	if(access(user_home, 00) != 0){
		gchar* tp = malloc(100);
		strcpy(tp, "cp -fr /opt/uniproxy/.uniconfig ");
		strcat(tp, user_home);
		system(tp);
		g_free(tp);
	}
	strcat(user_home, "/uniconfig.ini");
	g_key_file_load_from_file (uniconfig, user_home, flags, &error);
	if(error != NULL){	perror( _("Installtion incorrect!\n"));exit(1);}
	p_app = g_key_file_get_string(uniconfig, "backend", "path", NULL);
	//check_gae();
	g_spawn_command_line_async ("pgrep uniproxy", NULL);
	if(g_key_file_get_integer(uniconfig, "UI", "oneup", NULL)!=0 && detect_uniproxy())
	{	
		errdialog = gtk_message_dialog_new(GTK_WINDOW(window),
						GTK_DIALOG_DESTROY_WITH_PARENT,GTK_MESSAGE_ERROR,GTK_BUTTONS_CLOSE, _("There's another Uniproxy running."));
		gtk_dialog_run(GTK_DIALOG(errdialog));
		gtk_widget_destroy(errdialog);
	}
	else
	{	
		//Container
		vbox = gtk_vbox_new (FALSE, 5);
		hbox = gtk_hbox_new (TRUE, 2);
		menubar = gtk_menu_bar_new();
		gtk_container_add (GTK_CONTAINER (window), vbox);
		gtk_box_pack_start(GTK_BOX (vbox), menubar, FALSE, FALSE, 0);
	
		quit = gtk_menu_item_new_with_label(_("Exit"));
		menu = gtk_menu_item_new_with_label(_("Menu"));
		setup = gtk_menu_item_new_with_label(_("Setup"));
		help = gtk_menu_item_new_with_label(_("Help"));
		about = gtk_menu_item_new_with_label(_("About"));
		menu_menu = gtk_menu_new();
		help_menu = gtk_menu_new();
		gtk_container_add (GTK_CONTAINER (menubar), menu);
		gtk_container_add (GTK_CONTAINER (menubar), help);
		gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu), menu_menu);
		gtk_menu_item_set_submenu(GTK_MENU_ITEM(help), help_menu);
		gtk_container_add (GTK_CONTAINER (menu_menu), setup);
		gtk_container_add (GTK_CONTAINER (menu_menu), quit);
		gtk_container_add (GTK_CONTAINER (help_menu), about);
		g_signal_connect((gpointer)quit, "activate", G_CALLBACK(show_quit), NULL);
		g_signal_connect((gpointer)setup, "activate", G_CALLBACK(show_setup), NULL);
		g_signal_connect((gpointer)about, "activate", G_CALLBACK(show_about), NULL);
		lbutton = gtk_button_new_with_label("Start");
		label = gtk_label_new (_("Tips:Click the tray icon to hide or show main window."));
		gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
		gtk_box_pack_start(GTK_BOX (vbox), label, TRUE, TRUE, 1);
		gtk_box_pack_start(GTK_BOX (hbox), lbutton, TRUE, TRUE, 0);
		g_signal_connect(G_OBJECT(lbutton), "clicked",
			G_CALLBACK(e_proxy), NULL);
		button = gtk_button_new_with_label(_("Hide"));
		gtk_box_pack_start(GTK_BOX (hbox), button, TRUE, TRUE, 0);
		g_signal_connect(G_OBJECT(button), "clicked",
			G_CALLBACK(show_or_hide), NULL);
		gtk_box_pack_start(GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
		tray_icon = gtk_status_icon_new(); 
		gtk_status_icon_set_from_file(tray_icon, "/opt/uniproxy/uniproxy.png"); 
		gtk_status_icon_set_tooltip_text(tray_icon, _("Click to hide or show main window.")); 
		gtk_status_icon_set_title (tray_icon, "Uniproxy-GUI");
		gtk_status_icon_set_visible(tray_icon, TRUE);
		g_signal_connect(GTK_STATUS_ICON (tray_icon), "button-press-event",	G_CALLBACK(show_or_hide), NULL);
	
		//Main loop
		if(g_key_file_get_integer(uniconfig, "UI", "skiptaskbar", NULL)!=0)	gtk_window_set_skip_taskbar_hint(GTK_WINDOW(window), TRUE);
		gtk_widget_show_all (window);
		if(g_key_file_get_integer(uniconfig, "UI", "visible", NULL)==0)	gtk_widget_hide(window);
		if(g_key_file_get_integer(uniconfig, "proxy", "autostart", NULL)!=0)	e_proxy(NULL, NULL);
		gtk_main();
	}
	return 0;
}
